# Temporary hack to package dependencies on Linux.
# the macro names are kept similar to mac osx despite using 
# patchelf instead of otool (which is not available on linux)
# for ease of comparison maintenance. Same with commented lines

set(libdir "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@")

macro(install_name_tool_add_rpath lib)
    execute_process(COMMAND bash "-c" "patchelf --force-rpath --set-rpath '$ORIGIN/:$ORIGIN/../../sdk/Simbody/lib' '${libdir}/lib${lib}'"
        OUTPUT_VARIABLE res)
    message(STATUS "patchelf --set-rpath '$ORIGIN/:../../sdk/Simbody/lib' '${libdir}/lib${lib}' '${res}'")
endmacro()

macro(install_name_tool_delete_rpath lib)
    execute_process(COMMAND bash "-c" "patchelf --remove-rpath '${libdir}/lib${lib}'"
        OUTPUT_VARIABLE res)
    message(STATUS "patchelf --remove-rpath '${libdir}/lib${lib}' '${res}'")
endmacro()

macro(install_name_tool_print_rpath lib)
    execute_process(COMMAND bash "-c" "patchelf --print-rpath '${libdir}/lib${lib}'"
        OUTPUT_VARIABLE created_rpath)
    message(STATUS "RPATH = '${created_rpath}'")
endmacro()

if (OPENSIM_WITH_CASADI)

    install_name_tool_print_rpath(ipopt.so)
    install_name_tool_delete_rpath(ipopt.so)
    install_name_tool_add_rpath(ipopt.so)
    install_name_tool_print_rpath(ipopt.so)

    install_name_tool_print_rpath(coinmumps.so.3)
    install_name_tool_delete_rpath(coinmumps.so.3)
    install_name_tool_add_rpath(coinmumps.so.3)
    install_name_tool_print_rpath(coinmumps.so.3)

    install_name_tool_print_rpath(metis.so)
    install_name_tool_delete_rpath(metis.so)
    install_name_tool_add_rpath(metis.so)
    install_name_tool_print_rpath(metis.so)

    install_name_tool_print_rpath(casadi_conic_nlpsol.so)
    install_name_tool_delete_rpath(casadi_conic_nlpsol.so)
    install_name_tool_add_rpath(casadi_conic_nlpsol.so)
    install_name_tool_print_rpath(casadi_conic_nlpsol.so)

    install_name_tool_print_rpath(casadi_conic_nlpsol.so.3.7)
    install_name_tool_delete_rpath(casadi_conic_nlpsol.so.3.7)
    install_name_tool_add_rpath(casadi_conic_nlpsol.so.3.7)
    install_name_tool_print_rpath(casadi_conic_nlpsol.so.3.7)

    install_name_tool_print_rpath(casadi_conic_qrqp.so)
    install_name_tool_delete_rpath(casadi_conic_qrqp.so)
    install_name_tool_add_rpath(casadi_conic_qrqp.so)
    install_name_tool_print_rpath(casadi_conic_qrqp.so)

    install_name_tool_print_rpath(casadi_conic_qrqp.so.3.7)
    install_name_tool_delete_rpath(casadi_conic_qrqp.so.3.7)
    install_name_tool_add_rpath(casadi_conic_qrqp.so.3.7)
    install_name_tool_print_rpath(casadi_conic_qrqp.so.3.7)

    install_name_tool_print_rpath(casadi_importer_shell.so)
    install_name_tool_delete_rpath(casadi_importer_shell.so)
    install_name_tool_add_rpath(casadi_importer_shell.so)
    install_name_tool_print_rpath(casadi_importer_shell.so)

    install_name_tool_print_rpath(casadi_importer_shell.so.3.7)
    install_name_tool_delete_rpath(casadi_importer_shell.so.3.7)
    install_name_tool_add_rpath(casadi_importer_shell.so.3.7)
    install_name_tool_print_rpath(casadi_importer_shell.so.3.7)

    install_name_tool_print_rpath(casadi_integrator_collocation.so)
    install_name_tool_delete_rpath(casadi_integrator_collocation.so)
    install_name_tool_add_rpath(casadi_integrator_collocation.so)
    install_name_tool_print_rpath(casadi_integrator_collocation.so)

    install_name_tool_print_rpath(casadi_integrator_collocation.so.3.7)
    install_name_tool_delete_rpath(casadi_integrator_collocation.so.3.7)
    install_name_tool_add_rpath(casadi_integrator_collocation.so.3.7)
    install_name_tool_print_rpath(casadi_integrator_collocation.so.3.7)

    install_name_tool_print_rpath(casadi_integrator_cvodes.so)
    install_name_tool_delete_rpath(casadi_integrator_cvodes.so)
    install_name_tool_add_rpath(casadi_integrator_cvodes.so)
    install_name_tool_print_rpath(casadi_integrator_cvodes.so)

    install_name_tool_print_rpath(casadi_integrator_cvodes.so.3.7)
    install_name_tool_delete_rpath(casadi_integrator_cvodes.so.3.7)
    install_name_tool_add_rpath(casadi_integrator_cvodes.so.3.7)
    install_name_tool_print_rpath(casadi_integrator_cvodes.so.3.7)

    install_name_tool_print_rpath(casadi_integrator_idas.so)
    install_name_tool_delete_rpath(casadi_integrator_idas.so)
    install_name_tool_add_rpath(casadi_integrator_idas.so)
    install_name_tool_print_rpath(casadi_integrator_idas.so)

    install_name_tool_print_rpath(casadi_integrator_idas.so.3.7)
    install_name_tool_delete_rpath(casadi_integrator_idas.so.3.7)
    install_name_tool_add_rpath(casadi_integrator_idas.so.3.7)
    install_name_tool_print_rpath(casadi_integrator_idas.so.3.7)

    install_name_tool_print_rpath(casadi_integrator_rk.so)
    install_name_tool_delete_rpath(casadi_integrator_rk.so)
    install_name_tool_add_rpath(casadi_integrator_rk.so)
    install_name_tool_print_rpath(casadi_integrator_rk.so)

    install_name_tool_print_rpath(casadi_integrator_rk.so.3.7)
    install_name_tool_delete_rpath(casadi_integrator_rk.so.3.7)
    install_name_tool_add_rpath(casadi_integrator_rk.so.3.7)
    install_name_tool_print_rpath(casadi_integrator_rk.so.3.7)

    install_name_tool_print_rpath(casadi_interpolant_bspline.so)
    install_name_tool_delete_rpath(casadi_interpolant_bspline.so)
    install_name_tool_add_rpath(casadi_interpolant_bspline.so)
    install_name_tool_print_rpath(casadi_interpolant_bspline.so)

    install_name_tool_print_rpath(casadi_interpolant_bspline.so.3.7)
    install_name_tool_delete_rpath(casadi_interpolant_bspline.so.3.7)
    install_name_tool_add_rpath(casadi_interpolant_bspline.so.3.7)
    install_name_tool_print_rpath(casadi_interpolant_bspline.so.3.7)

    install_name_tool_print_rpath(casadi_interpolant_linear.so)
    install_name_tool_delete_rpath(casadi_interpolant_linear.so)
    install_name_tool_add_rpath(casadi_interpolant_linear.so)
    install_name_tool_print_rpath(casadi_interpolant_linear.so)

    install_name_tool_print_rpath(casadi_interpolant_linear.so.3.7)
    install_name_tool_delete_rpath(casadi_interpolant_linear.so.3.7)
    install_name_tool_add_rpath(casadi_interpolant_linear.so.3.7)
    install_name_tool_print_rpath(casadi_interpolant_linear.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_csparsecholesky.so)
    install_name_tool_delete_rpath(casadi_linsol_csparsecholesky.so)
    install_name_tool_add_rpath(casadi_linsol_csparsecholesky.so)
    install_name_tool_print_rpath(casadi_linsol_csparsecholesky.so)

    install_name_tool_print_rpath(casadi_linsol_csparsecholesky.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_csparsecholesky.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_csparsecholesky.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_csparsecholesky.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_csparse.so)
    install_name_tool_delete_rpath(casadi_linsol_csparse.so)
    install_name_tool_add_rpath(casadi_linsol_csparse.so)
    install_name_tool_print_rpath(casadi_linsol_csparse.so)

    install_name_tool_print_rpath(casadi_linsol_csparse.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_csparse.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_csparse.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_csparse.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_ldl.so)
    install_name_tool_delete_rpath(casadi_linsol_ldl.so)
    install_name_tool_add_rpath(casadi_linsol_ldl.so)
    install_name_tool_print_rpath(casadi_linsol_ldl.so)

    install_name_tool_print_rpath(casadi_linsol_ldl.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_ldl.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_ldl.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_ldl.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_lsqr.so)
    install_name_tool_delete_rpath(casadi_linsol_lsqr.so)
    install_name_tool_add_rpath(casadi_linsol_lsqr.so)
    install_name_tool_print_rpath(casadi_linsol_lsqr.so)

    install_name_tool_print_rpath(casadi_linsol_lsqr.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_lsqr.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_lsqr.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_lsqr.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_qr.so)
    install_name_tool_delete_rpath(casadi_linsol_qr.so)
    install_name_tool_add_rpath(casadi_linsol_qr.so)
    install_name_tool_print_rpath(casadi_linsol_qr.so)

    install_name_tool_print_rpath(casadi_linsol_qr.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_qr.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_qr.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_qr.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_symbolicqr.so)
    install_name_tool_delete_rpath(casadi_linsol_symbolicqr.so)
    install_name_tool_add_rpath(casadi_linsol_symbolicqr.so)
    install_name_tool_print_rpath(casadi_linsol_symbolicqr.so)

    install_name_tool_print_rpath(casadi_linsol_symbolicqr.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_symbolicqr.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_symbolicqr.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_symbolicqr.so.3.7)

    install_name_tool_print_rpath(casadi_linsol_tridiag.so)
    install_name_tool_delete_rpath(casadi_linsol_tridiag.so)
    install_name_tool_add_rpath(casadi_linsol_tridiag.so)
    install_name_tool_print_rpath(casadi_linsol_tridiag.so)

    install_name_tool_print_rpath(casadi_linsol_tridiag.so.3.7)
    install_name_tool_delete_rpath(casadi_linsol_tridiag.so.3.7)
    install_name_tool_add_rpath(casadi_linsol_tridiag.so.3.7)
    install_name_tool_print_rpath(casadi_linsol_tridiag.so.3.7)

    install_name_tool_print_rpath(casadi_nlpsol_ipopt.so)
    install_name_tool_delete_rpath(casadi_nlpsol_ipopt.so)
    install_name_tool_add_rpath(casadi_nlpsol_ipopt.so)
    install_name_tool_print_rpath(casadi_nlpsol_ipopt.so)

    install_name_tool_print_rpath(casadi_nlpsol_ipopt.so.3.7)
    install_name_tool_delete_rpath(casadi_nlpsol_ipopt.so.3.7)
    install_name_tool_add_rpath(casadi_nlpsol_ipopt.so.3.7)
    install_name_tool_print_rpath(casadi_nlpsol_ipopt.so.3.7)

    install_name_tool_print_rpath(casadi_nlpsol_qrsqp.so)
    install_name_tool_delete_rpath(casadi_nlpsol_qrsqp.so)
    install_name_tool_add_rpath(casadi_nlpsol_qrsqp.so)
    install_name_tool_print_rpath(casadi_nlpsol_qrsqp.so)

    install_name_tool_print_rpath(casadi_nlpsol_ipopt.so.3.7)
    install_name_tool_delete_rpath(casadi_nlpsol_ipopt.so.3.7)
    install_name_tool_add_rpath(casadi_nlpsol_ipopt.so.3.7)
    install_name_tool_print_rpath(casadi_nlpsol_ipopt.so.3.7)

    install_name_tool_print_rpath(casadi_nlpsol_qrsqp.so)
    install_name_tool_delete_rpath(casadi_nlpsol_qrsqp.so)
    install_name_tool_add_rpath(casadi_nlpsol_qrsqp.so)
    install_name_tool_print_rpath(casadi_nlpsol_qrsqp.so)

    install_name_tool_print_rpath(casadi_nlpsol_qrsqp.so.3.7)
    install_name_tool_delete_rpath(casadi_nlpsol_qrsqp.so.3.7)
    install_name_tool_add_rpath(casadi_nlpsol_qrsqp.so.3.7)
    install_name_tool_print_rpath(casadi_nlpsol_qrsqp.so.3.7)

    install_name_tool_print_rpath(casadi_nlpsol_scpgen.so)
    install_name_tool_delete_rpath(casadi_nlpsol_scpgen.so)
    install_name_tool_add_rpath(casadi_nlpsol_scpgen.so)
    install_name_tool_print_rpath(casadi_nlpsol_scpgen.so)

    install_name_tool_print_rpath(casadi_nlpsol_scpgen.so.3.7)
    install_name_tool_delete_rpath(casadi_nlpsol_scpgen.so.3.7)
    install_name_tool_add_rpath(casadi_nlpsol_scpgen.so.3.7)
    install_name_tool_print_rpath(casadi_nlpsol_scpgen.so.3.7)

    install_name_tool_print_rpath(casadi_nlpsol_sqpmethod.so)
    install_name_tool_delete_rpath(casadi_nlpsol_sqpmethod.so)
    install_name_tool_add_rpath(casadi_nlpsol_sqpmethod.so)
    install_name_tool_print_rpath(casadi_nlpsol_sqpmethod.so)

    install_name_tool_print_rpath(casadi_nlpsol_sqpmethod.so.3.7)
    install_name_tool_delete_rpath(casadi_nlpsol_sqpmethod.so.3.7)
    install_name_tool_add_rpath(casadi_nlpsol_sqpmethod.so.3.7)
    install_name_tool_print_rpath(casadi_nlpsol_sqpmethod.so.3.7)

    install_name_tool_print_rpath(casadi_rootfinder_fast_newton.so)
    install_name_tool_delete_rpath(casadi_rootfinder_fast_newton.so)
    install_name_tool_add_rpath(casadi_rootfinder_fast_newton.so)
    install_name_tool_print_rpath(casadi_rootfinder_fast_newton.so)

    install_name_tool_print_rpath(casadi_nlpsol_sqpmethod.so.3.7)
    install_name_tool_delete_rpath(casadi_nlpsol_sqpmethod.so.3.7)
    install_name_tool_add_rpath(casadi_nlpsol_sqpmethod.so.3.7)
    install_name_tool_print_rpath(casadi_nlpsol_sqpmethod.so.3.7)

    install_name_tool_print_rpath(casadi_rootfinder_fast_newton.so)
    install_name_tool_delete_rpath(casadi_rootfinder_fast_newton.so)
    install_name_tool_add_rpath(casadi_rootfinder_fast_newton.so)
    install_name_tool_print_rpath(casadi_rootfinder_fast_newton.so)

    install_name_tool_print_rpath(casadi_rootfinder_fast_newton.so.3.7)
    install_name_tool_delete_rpath(casadi_rootfinder_fast_newton.so.3.7)
    install_name_tool_add_rpath(casadi_rootfinder_fast_newton.so.3.7)
    install_name_tool_print_rpath(casadi_rootfinder_fast_newton.so.3.7)

    install_name_tool_print_rpath(casadi_rootfinder_kinsol.so)
    install_name_tool_delete_rpath(casadi_rootfinder_kinsol.so)
    install_name_tool_add_rpath(casadi_rootfinder_kinsol.so)
    install_name_tool_print_rpath(casadi_rootfinder_kinsol.so)

    install_name_tool_print_rpath(casadi_rootfinder_kinsol.so.3.7)
    install_name_tool_delete_rpath(casadi_rootfinder_kinsol.so.3.7)
    install_name_tool_add_rpath(casadi_rootfinder_kinsol.so.3.7)
    install_name_tool_print_rpath(casadi_rootfinder_kinsol.so.3.7)

    install_name_tool_print_rpath(casadi_rootfinder_newton.so)
    install_name_tool_delete_rpath(casadi_rootfinder_newton.so)
    install_name_tool_add_rpath(casadi_rootfinder_newton.so)
    install_name_tool_print_rpath(casadi_rootfinder_newton.so)

    install_name_tool_print_rpath(casadi_rootfinder_newton.so.3.7)
    install_name_tool_delete_rpath(casadi_rootfinder_newton.so.3.7)
    install_name_tool_add_rpath(casadi_rootfinder_newton.so.3.7)
    install_name_tool_print_rpath(casadi_rootfinder_newton.so.3.7)

    install_name_tool_print_rpath(casadi_rootfinder_nlpsol.so)
    install_name_tool_delete_rpath(casadi_rootfinder_nlpsol.so)
    install_name_tool_add_rpath(casadi_rootfinder_nlpsol.so)
    install_name_tool_print_rpath(casadi_rootfinder_nlpsol.so)

    install_name_tool_print_rpath(casadi_rootfinder_nlpsol.so.3.7)
    install_name_tool_delete_rpath(casadi_rootfinder_nlpsol.so.3.7)
    install_name_tool_add_rpath(casadi_rootfinder_nlpsol.so.3.7)
    install_name_tool_print_rpath(casadi_rootfinder_nlpsol.so.3.7)

    install_name_tool_print_rpath(casadi.so)
    install_name_tool_delete_rpath(casadi.so)
    install_name_tool_add_rpath(casadi.so)
    install_name_tool_print_rpath(casadi.so)

    install_name_tool_print_rpath(casadi.so.3.7)
    install_name_tool_delete_rpath(casadi.so.3.7)
    install_name_tool_add_rpath(casadi.so.3.7)
    install_name_tool_print_rpath(casadi.so.3.7)

    install_name_tool_print_rpath(casadi_sundials_common.so)
    install_name_tool_delete_rpath(casadi_sundials_common.so)
    install_name_tool_add_rpath(casadi_sundials_common.so)
    install_name_tool_print_rpath(casadi_sundials_common.so)

    install_name_tool_print_rpath(casadi_sundials_common.so.3.7)
    install_name_tool_delete_rpath(casadi_sundials_common.so.3.7)
    install_name_tool_add_rpath(casadi_sundials_common.so.3.7)
    install_name_tool_print_rpath(casadi_sundials_common.so.3.7)

    install_name_tool_print_rpath(casadi_xmlfile_tinyxml.so)
    install_name_tool_delete_rpath(casadi_xmlfile_tinyxml.so)
    install_name_tool_add_rpath(casadi_xmlfile_tinyxml.so)
    install_name_tool_print_rpath(casadi_xmlfile_tinyxml.so)

    install_name_tool_print_rpath(casadi_xmlfile_tinyxml.so.3.7)
    install_name_tool_delete_rpath(casadi_xmlfile_tinyxml.so.3.7)
    install_name_tool_add_rpath(casadi_xmlfile_tinyxml.so.3.7)
    install_name_tool_print_rpath(casadi_xmlfile_tinyxml.so.3.7)

endif()


